package com.os.ordersystem.controller;

import com.os.ordersystem.bean.Food;
import com.os.ordersystem.bean.Notice;
import com.os.ordersystem.bean.Page;
import com.os.ordersystem.bean.ResultDTO;
import com.os.ordersystem.service.NoticeService;
import com.os.ordersystem.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/notice")
public class NoticeController {

    @Resource
    private NoticeService noticeService;

    @Resource
    private UserService userService;

    //添加食品
    @PostMapping("/add")
    public ResultDTO addNotice(@RequestBody Notice notice) {
        int i = noticeService.addNotice(notice);
        return ResultDTO.success();
    }

    //分页
    @GetMapping("/getByPage")
    public ResultDTO getByPage(@RequestParam String title,
                               @RequestParam Integer page,@RequestParam Integer size) {

        Map<String, Object> params = new HashMap<>();
        params.put("page", (page-1)*size);
        params.put("size", size);
        params.put("NoticeTitle", title);

        List<Notice> byPage = noticeService.getByPage(params);

        Page<Notice> noticePage = new Page<>();

        long total = noticeService.getTotal(title);
        noticePage.setRows(byPage);
        noticePage.setTotal(total);
        noticePage.setPageNum(page);
        noticePage.setPageSize(size);

        return ResultDTO.success(noticePage);
    }

    //删除食品
    @DeleteMapping("/del")
    public ResultDTO delNoticeById(@RequestParam Integer id) {
        int i = noticeService.delNoticeById(id);
        if (i == 1 ) {
            return ResultDTO.success();
        }else {
            return ResultDTO.erro();
        }
    }

    //修改食品
    @PostMapping("/update")
    public ResultDTO updateNotice(@RequestBody Notice notice) {

        noticeService.updateNotice(notice);

        return ResultDTO.success();
    }




}
